# -*- coding: utf-8 -*-
import itertools

from matplotlib import pyplot
from numpy import unique

from program_top.utilities.widgets.plain_chart import plain_chart

class scattered_xy_chart(plain_chart):
	'''
	散点图
	'''
	def __init__(self,file_fullname,x_name,y_name,x_series_data,y_series_data):
		super(scattered_xy_chart, self).__init__()
		'''
		concated=pandas.concat([x_series_data,y_series_data], axis=1)

		target_x_reshaped=concated.ix[:, 0]
		target_y_reshaped=concated.ix[:, 1]
		'''
		new_scattered=pyplot.scatter(x_series_data, y_series_data,marker='.')
		current_figure=new_scattered.get_figure()
		current_figure.axes[0].set_xlabel(x_name)
		current_figure.axes[0].set_ylabel(y_name)
		current_figure.axes[0].set_title(x_name+'_vs_'+y_name)
		current_figure.tight_layout()
		current_figure.savefig(file_fullname,dpi=200)
		pyplot.clf()
		pyplot.cla()
		pyplot.close()
		pass
	pass

def show_single_series(data_name,serial_data):
	path_collection=pyplot.scatter(range(serial_data.shape[0]), serial_data,marker='.',s=serial_data.shape[0]*[0.1])
	current_figure=path_collection.get_figure()
	current_figure.tight_layout()
	#current_figure.show()
	current_figure.savefig(data_name+"_single_series.png",dpi=2000)
	pyplot.clf()
	pyplot.cla()
	pyplot.close()

def show_coloured_series(data_name,colour_label_series,serial_data):
	path_collection=pyplot.scatter(range(serial_data.shape[0]), serial_data,marker='.',s=serial_data.shape[0]*[10],color=colour_label_series)
	current_figure=path_collection.get_figure()
	current_figure.tight_layout()
	#current_figure.show()
	current_figure.savefig(data_name+"_coloured_series.png",dpi=2000)
	pyplot.clf()
	pyplot.cla()
	pyplot.close()

def map_status_to_color_series(label_series):
	levels=unique(label_series)
	colors=itertools.cycle(["r", "b", "g"])
	colour_dict={each_level: next(colors) for each_level in levels}
	colour_series=[colour_dict[each_point] for each_point in label_series]
	return colour_series

def generate_scatters_with_each_label(total_series,label_series):
	point_count=0
	total_map={}
	
	for each_label,each_value in zip(label_series,total_series):
		if not total_map.__contains__(each_label):
			total_map[each_label]={'x':[point_count],'y':[each_value]}
		else:
			total_map[each_label]['x'].append(point_count)
			total_map[each_label]['y'].append(each_value)
			pass
		point_count+=1
		pass
	
	for each_category in total_map.keys():
		current_cat=scattered_xy_chart(file_fullname='cat_'+str(each_category)+'.png',
						   x_name='index',
						   y_name="price",
						   x_series_data=total_map[each_category]['x'],
						   y_series_data=total_map[each_category]['y']
						   )
		pass
	
	pass